<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="90%">
		<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col :span="12">
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" placeholder="请输入名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="thumb">
						<template #label>
							相册封面图<br />
							宽高比例13:8<br />
							建议(260*160)<br />
							大小不超过2M
						</template>
						<image-upload-cOS v-model="form.thumb" ref="imageUploadThumbRef" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="名称" prop="details">
						<editor :is-disable="false" v-model="form.details" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm">确定</el-button>
				<el-button @click="onCancel">取消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';

import Editor from '/@/components/editor/index.vue';

export default {
	name: 'generalDialog',
	components: { Editor },
	setup() {
		const state: any = reactive({
			title: 'generalDialog',
			open: false,
			form: {},
			rules: {
				name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
			}, // 表单校验
			submitLoading: false,
		});

		const formRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					console.log('state.form', state.form);

					onCancel();
					callback();
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			callback = fn;
			state.open = true;
			reset();
		};
		// 表单重置
		const reset = () => {
			state.form = {
				name: '',
				thumb: '',
				details: '',
			};
			(formRef.value as any)?.resetFields();
		};

		return {
			formRef,
			onSubmitForm,
			openDialog,
			onCancel,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
